package com.tgd.RingtoneContactManager;

import android.content.Context;
import android.text.TextPaint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * http://developer.android.com/resources/samples/ApiDemos/src/com/example/
 * android/apis/view/List14.html
 */
public class RingtoneAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Ringtone[] _rings;

    public RingtoneAdapter(Context context, Ringtone[] list) {
        // Cache the LayoutInflate to avoid asking for a new one each time.
        mInflater = LayoutInflater.from(context);
        _rings = list;
    }

    /**
     * The number of items in the list is determined by the number of speeches
     * in our array.
     * 
     * @see android.widget.ListAdapter#getCount()
     */
    public int getCount() {
        return (_rings == null ? 0 : _rings.length);
    }

    /**
     * Since the data comes from an array, just returning the index is sufficent
     * to get at the data. If we were using a more complex data structure, we
     * would return whatever object represents one row in the list.
     * 
     * @see android.widget.ListAdapter#getItem(int)
     */
    public Object getItem(int position) {
        return _rings[position];
    }

    /**
     * Use the array index as a unique id.
     * 
     * @see android.widget.ListAdapter#getItemId(int)
     */
    public long getItemId(int position) {
        return position;
    }

    /**
     * Make a view to hold each row.
     * 
     * @see android.widget.ListAdapter#getView(int, android.view.View,
     *      android.view.ViewGroup)
     */
    public View getView(int position, View convertView, ViewGroup parent) {
        // A ViewHolder keeps references to children views to avoid
        // unneccessary calls to findViewById() on each row.
        ViewHolder holder;

        // When convertView is not null, we can reuse it directly, there is
        // no need to reinflate it. We only inflate a new View when the
        // convertView supplied by ListView is null.
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.ringrow, null);

            // Creates a ViewHolder and store references to the two children
            // views
            // we want to bind data to.
            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.bob1);
            holder.icon = (TextView) convertView.findViewById(R.id.bob2);

            convertView.setTag(holder);
        } else {
            // Get the ViewHolder back to get fast access to the TextView
            // and
            // the ImageView.
            holder = (ViewHolder) convertView.getTag();
        }

        // Bind the data efficiently with the holder.
        Ringtone r = _rings[position];
        holder.rt = r;
        convertView.setTag(holder);// TODO find a better way to pass the
                                   // selected item
        holder.text.setText(r.getName().concat(r.isDefault() ? " (default)" : ""));

        String text = TextUtils.join(", ", r.getContacts());
        TextPaint p = holder.icon.getPaint();
        float textWidth = parent.getMeasuredWidth() - holder.icon.getPaddingLeft()
                - holder.icon.getPaddingRight() - 1;

        holder.icon.setText(TextUtils.commaEllipsize(text, p, textWidth, "1 more", "%d more"));

        return convertView;
    }

    class ViewHolder {
        TextView text;
        TextView icon;
        Ringtone rt;
    }

}
